package cn.lili.listener;

import cn.hutool.json.JSONUtil;
import cn.lili.modules.order.aftersale.serviceimpl.AfterSaleServiceImpl;
import cn.lili.rocketmq.tags.FileTagsEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 文件消息监听类
 *
 * @author moxingjian
 * @version 1.0
 * @date 2023-11-13 11:14
 * <p>
 * 版本更新：
 * 更新版本号    更新人      更新时间      更新内容
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "${lili.data.rocketmq.file-topic}", consumerGroup = "${lili.data.rocketmq.file-group}")
public class FileMessageListener implements RocketMQListener<MessageExt> {


    @Resource
    private AfterSaleServiceImpl afterSaleService;

    @Override
    public void onMessage(MessageExt messageExt) {
        if (FileTagsEnum.AFTER_SALE.equals(FileTagsEnum.valueOf(messageExt.getTags()))) {
            try {
                Map map = JSONUtil.toBean(new String(messageExt.getBody()), Map.class);
                // 迁移文件到新路径，更新原来的文件URL
                afterSaleService.updateFileUrl(map);
            } catch (Exception e) {
                log.error("文件消息回调执行异常：{}", new String(messageExt.getBody()), e);
            }

        }
    }
}
